﻿using System;
using System.Collections.Generic;
using System.Web;

using System.IO;
using System.Xml;
using System.Data;
using System.Text;
using GreatB2C.Utility;
using System.Configuration;

namespace GreatB2C.Web.Component
{
    public class DefaultTemplateFields
    {
        /// <summary>
        /// 声明XML文档
        /// </summary>
        XmlDocument xmlDoc;

        private string field = "";
        /// <summary>
        /// 字段XML信息
        /// </summary>
        public string Field
        {
            get { return field; }
            set { field = value; }
        }

        DataTable dtFields = null;

        /// <summary>
        /// 加入字段信息
        /// </summary>
        /// <param name="dr"></param>
        public void AddRow( DataRow dr )
        {
            if ( dtFields != null )
            {
                dtFields.Rows.Add( dr.ItemArray );
            }
        }

        /// <summary>
        /// 初始化数据表
        /// </summary>
        public DefaultTemplateFields( )
        {
            if ( dtFields == null )
            {
                dtFields = new DataTable( );
                dtFields.Columns.Add( "Id" );
                dtFields.Columns.Add( "Disabled" );
                dtFields.Columns.Add( new DataColumn( "OrderId" , typeof( int ) ) );
                dtFields.Columns.Add( "FieldName" );
                dtFields.Columns.Add( "EnableShowOnSearchForm" );
                dtFields.Columns.Add( "EnableFilter" );
                dtFields.Columns.Add( "FieldAlias" );
                dtFields.Columns.Add( "FieldLevel" );
                dtFields.Columns.Add( "Tips" );
                dtFields.Columns.Add( "Description" );
                dtFields.Columns.Add( "FieldType" );
                dtFields.Columns.Add( "Settings" );
                dtFields.Columns.Add( "EnableNull" );
                dtFields.Columns.Add( "DefaultValue" );
                dtFields.Columns.Add( "ModelId" );
            }
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        private void LoadXml( )
        {
            if ( Field.Trim( ) != "" )
            {
                xmlDoc = new XmlDocument( );
                StringReader strReader = new StringReader( Field );
                xmlDoc.Load( strReader );
            }
        }

        /// <summary>
        /// 获取模型数据集合
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable( string FieldLevel )
        {
            if ( Field.Trim( ) != "" )
            {
                try
                {
                    LoadXml( );

                    XmlNode xmldocSelect=xmlDoc.SelectSingleNode( "ArrayOfFieldInfo" );

                    foreach ( XmlNode node in xmldocSelect.ChildNodes )     // FieldInfo 
                    {                        
                        DataRow dr = dtFields.NewRow( );

                        if ( node.Name == "FieldInfo" )
                        {
                            dr[ "Id" ] = node.Attributes[ "Id" ].Value.ToString( );
                            dr[ "Disabled" ] = Convert.ToBoolean( node.Attributes[ "Disabled" ].Value.ToString( ) );
                            dr[ "OrderId" ] = Convert.ToInt32( node.Attributes[ "OrderId" ].Value );
                        }

                        XmlNodeList xnl = node.ChildNodes;

                        bool isCorrupt = false;

                        foreach ( XmlNode xn in xnl )
                        {
                            switch ( xn.Name )
                            {
                                case "FieldName":
                                    dr[ "FieldName" ] = xn.InnerXml;
                                    break;
                                case "EnableShowOnSearchForm":
                                    dr[ "EnableShowOnSearchForm" ] = xn.InnerXml;
                                    break;
                                case "EnableFilter":
                                    dr[ "EnableFilter" ] = xn.InnerXml;
                                    break;
                                case "FieldAlias":
                                    dr[ "FieldAlias" ] = xn.InnerXml;
                                    break;
                                case "FieldLevel":
                                    {
                                        if ( FieldLevel.Trim( ) != "" )
                                        {
                                            if ( xn.InnerXml != FieldLevel )
                                            {
                                                isCorrupt = true;
                                                break;
                                            }
                                        }

                                        dr[ "FieldLevel" ] = xn.InnerXml;
                                    }
                                    break;
                                case "Tips":
                                    dr[ "Tips" ] = xn.InnerXml;
                                    break;
                                case "Description":
                                    dr[ "Description" ] = xn.InnerXml;
                                    break;
                                case "FieldType":
                                    dr[ "FieldType" ] = xn.InnerXml;
                                    break;
                                case "Settings":
                                    {
                                        string value = "";
                                        foreach ( XmlNode xntmp in xn.ChildNodes )
                                        {
                                            value += xntmp.InnerXml + ",";
                                        }
                                        dr[ "Settings" ] = value.Trim( ',' );
                                    }
                                    break;
                                case "EnableNull":
                                    dr[ "EnableNull" ] = xn.InnerXml;
                                    break;
                                case "DefaultValue":
                                    dr[ "DefaultValue" ] = xn.InnerXml;
                                    break;
                                case "ModelId":
                                    dr[ "ModelId" ] = xn.InnerXml;
                                    break;
                            }
                        }

                        if ( !isCorrupt )
                            dtFields.Rows.Add( dr );
                    }

                    return dtFields;
                }
                catch
                {
                    return null;
                }
            }

            return null;
        }

        /// <summary>
        /// 根据字段数据集合，生成字段信息XML字符串
        /// </summary>
        /// <param name="dtData">字段集合</param>
        /// <returns></returns>
        public string GetXMLData( )
        {
            if ( dtFields != null && dtFields.Rows.Count > 0 )
            {
                StringBuilder sbXML = new StringBuilder( );
                sbXML.Append( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + DataDefine.NewLine );
                sbXML.Append( "<ArrayOfFieldInfo>" + DataDefine.NewLine );

                foreach ( DataRow dr in dtFields.Rows )
                {
                    sbXML.Append( "<FieldInfo Id=\"" + dr[ "Id" ].ToString( ) + "\" Disabled=\"" + dr[ "Disabled" ].ToString( ) + "\" OrderId=\"" + dr[ "OrderId" ].ToString( ) + "\">" + DataDefine.NewLine );
                    sbXML.Append( "     <FieldName>" + dr[ "FieldName" ].ToString( ) + "</FieldName>" + DataDefine.NewLine );
                    sbXML.Append( "     <EnableShowOnSearchForm>" + dr[ "EnableShowOnSearchForm" ].ToString( ) + "</EnableShowOnSearchForm>" + DataDefine.NewLine );
                    sbXML.Append( "     <EnableFilter>" + dr[ "EnableFilter" ].ToString( ) + "</EnableFilter>" + DataDefine.NewLine );
                    sbXML.Append( "     <FieldAlias>" + dr[ "FieldAlias" ].ToString( ) + "</FieldAlias>" + DataDefine.NewLine );
                    sbXML.Append( "     <FieldLevel>" + dr[ "FieldLevel" ].ToString( ) + "</FieldLevel>" + DataDefine.NewLine );
                    sbXML.Append( "     <Tips>" + dr[ "Tips" ].ToString( ) + "</Tips>" + DataDefine.NewLine );
                    sbXML.Append( "     <Description>" + dr[ "Description" ].ToString( ) + "</Description>" + DataDefine.NewLine );
                    sbXML.Append( "     <FieldType>" + dr[ "FieldType" ].ToString( ) + "</FieldType>" + DataDefine.NewLine );

                    string[] setting = dr[ "Settings" ].ToString( ).Split( ',' );
                    sbXML.Append( "     <Settings>" + DataDefine.NewLine );
                    foreach ( string set in setting )
                    {
                        sbXML.Append( "         <string>" + set + "</string>" + DataDefine.NewLine );
                    }
                    sbXML.Append( "     </Settings>" + DataDefine.NewLine );

                    sbXML.Append( "     <EnableNull>" + dr[ "EnableNull" ].ToString( ) + "</EnableNull>" + DataDefine.NewLine );
                    sbXML.Append( "     <DefaultValue>" + dr[ "DefaultValue" ].ToString( ) + "</DefaultValue>" + DataDefine.NewLine );
                    sbXML.Append( "     <ModelId>" + dr[ "ModelId" ].ToString( ) + "</ModelId>" + DataDefine.NewLine );
                    sbXML.Append( "</FieldInfo>" + DataDefine.NewLine );
                }

                sbXML.Append( "</ArrayOfFieldInfo>" + DataDefine.NewLine );
                return sbXML.ToString( );
            }

            return "";
        }
    }
}
